﻿// Copyright (c) ZhangPeihang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. 
using LaserMark.DataAccess.Models;

namespace LaserMark.DataAccess.Service;
public class CodeTemplateService : ICodeTemplateService
{
    private IDBManager DBManager { get; set; }

    public CodeTemplateService(IDBManager dBManager)
    {
        DBManager = dBManager;
    }

    public async Task<bool> Insert(MarkTemplate template)
    {
        using (var db = DBManager.Create())
        {
            var ret = await db.InsertAsync(template);

            return ret != null;
        }
    }

    public async Task<List<MarkTemplate>> GetAll()
    {
        using (var db = DBManager.Create())
        {
            return (await db.FetchAsync<MarkTemplate>()).OrderBy(s => s.Order).ToList();
        }
    }

    public async Task<MarkTemplate> GetByOrderNo(string orderNo)
    {
        using (var db = DBManager.Create())
        {
            return await db.SingleAsync<MarkTemplate>("where [Order]=@0", Convert.ToInt32(orderNo));
        }
    }

    public async Task<bool> Delete(MarkTemplate mark)
    {
        using (var db = DBManager.Create())
        {
            return (await db.DeleteAsync(mark)) > 0;
        }
    }
}
